package com.fieldchain.ex.fa100.utils.net.access;

import com.fieldchain.ex.fa100.access.MyApplication;
import com.fieldchain.ex.fa100.bean.event.BaseEvent;
import com.fieldchain.ex.fa100.utils.net.action.GetImageRequest;
import com.fieldchain.ex.fa100.utils.net.action.GetRequest;
import com.fieldchain.ex.fa100.utils.net.action.PostRequest;
import com.fieldchain.ex.fa100.utils.net.inter.BaseRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ImageRequest;
import com.fieldchain.ex.fa100.utils.net.tools.MyCookeiJar;
import com.fieldchain.ex.fa100.utils.net.tools.NetStatus;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import org.greenrobot.eventbus.EventBus;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;

/**
 * Created by biyunlong on 2016/9/5.
 */
public class HttpRequest {
    public static OkHttpClient okHttpClient;
    public static HttpRequest request;

    public void init() {
        if (null == okHttpClient) {
            OkHttpClient.Builder ClientBuilder = new OkHttpClient.Builder().cookieJar(new MyCookeiJar());
            ClientBuilder.readTimeout(60, TimeUnit.SECONDS);//读取超时
            ClientBuilder.connectTimeout(60, TimeUnit.SECONDS);//连接超时
            ClientBuilder.writeTimeout(60, TimeUnit.SECONDS);//写入超时
            okHttpClient = ClientBuilder.build();
        }
    }

    public void clearAll(){
        okHttpClient.dispatcher().cancelAll();
    }
    public static HttpRequest getInstance() {
        if(!NetStatus.isNetWorkConnected(MyApplication.getAppContext())){
            MyToast.show("连接失败，请检查网络");
        }
        if (null == request) {
            request = new HttpRequest();
        }
        return request;
    }
    public void getRequest(String url, String tags,final BaseRequest re ) {
        EventBus.getDefault().post(new BaseEvent(1,tags));
        GetRequest.getRequest(url, re, okHttpClient,tags);
    }
    public void getRequest(HashMap params,String url,String tags ,final BaseRequest re) {
        EventBus.getDefault().post(new BaseEvent(1,tags));
        url=getUrl(params,url);
        GetRequest.getRequest(url, re, okHttpClient,tags);
    }
    public void getRequest(String url,final BaseRequest re ) {
        EventBus.getDefault().post(new BaseEvent(1,"null"));
        GetRequest.getRequest(url, re, okHttpClient,"null");
    }
    public void getRequest(HashMap params,String url ,final BaseRequest re) {
        EventBus.getDefault().post(new BaseEvent(1,"null"));
        url=getUrl(params,url);
        GetRequest.getRequest(url, re, okHttpClient,"null");
    }

    public void getImage(String url, final ImageRequest re) {
        GetImageRequest.getRequest(url, re, okHttpClient,"null");
    }

    //普通接口
    public void postRequest(Map<String, String> params, String url,String tags, final BaseRequest re) {
        EventBus.getDefault().post(new BaseEvent(1,tags));
        PostRequest.postRequest(params, url, re, okHttpClient,tags);
    }
    //json
    public void postRequest(String json,String url,String tags, final BaseRequest re) {
        EventBus.getDefault().post(new BaseEvent(1,tags));
        PostRequest.postRequest(json,url, re, okHttpClient,tags);
    }


    //普通接口
    public void postRequest(Map<String, String> params, String url, final BaseRequest re) {
        EventBus.getDefault().post(new BaseEvent(1,"null"));
        PostRequest.postRequest(params, url, re, okHttpClient,"null");
    }
    //json
    public void postRequest(String json,String url, final BaseRequest re) {
        EventBus.getDefault().post(new BaseEvent(1,"null"));
        PostRequest.postRequest(json,url, re, okHttpClient,"null");
    }





    public String getUrl(HashMap params,String url){
        if (params != null) {
            Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> entry = it.next();
                url=url+"&"+entry.getKey()+"="+entry.getValue();
            }
            url= url.replaceFirst("&","?");
        }
        return url;
    }

}
